import Paging from '../../../wxjs/Paging.js'
import navToNext from '../../../wxjs/navToNext.js'
const app = getApp();

var pag = new Paging();

var data = {
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
};

var currentPage = {
  data,
  reqUrl: app.serAddrs.getRelateUserInfo,
  onLoad,
  firstCallBack,
  relateClass,
  checkItem,
  comfirm,
};

var page = Object.assign({}, pag.func(), currentPage);

Page(page);

function onLoad() {
  const that = this;
  // 初始化
  that.pageListInit();
}

function firstCallBack() {
  const that = this;
  that.relateClass();
}

function relateClass() {
  const that = this;
  var lists = that.data.lists;
  var prevPage = getCurrentPages()[getCurrentPages().length - 2] || {};

  if (prevPage.route) {
    var selRelates = prevPage.data.selRelates || [];
    if (selRelates.length > 0) {
      for (var i = 0; i < selRelates.length; i++) {
        for (var j = 0; j < lists.length; j++) {
          if (selRelates[i].relationAccount == lists[j].relationAccount) {
            lists[j].status = selRelates[i].status;
          }
        }
      }
    }
  }

  that.setData({
    lists,
    prevPage,
  })
}

function checkItem(event) {
  const that = this;
  const index = event.currentTarget.dataset.index;
  var lists = that.data.lists;

  lists.forEach((item) => {
    if(item)item.status = 0;
  })

  lists[index].status = lists[index].status ? 0 : 1;
  that.setData({
    lists,
  })
}

function comfirm() {
  const that = this;
  var lists = that.data.lists;
  var prevPage = that.data.prevPage;
  var selRelates = lists.filter(key => key.status);

  prevPage.setData({
    selRelates,
  })
  wx.navigateBack();
}